<template>
	<view class="activist-container" :data-theme="themeStyle">
		<scroll-view scroll-x="true" class="order-nav">
			<view v-for="(statusItem, statusIndex) in statusList" :key="statusIndex" class="uni-tab-item" :id="statusItem.id" :data-current="statusIndex" @click="ontabtap">
				<text class="uni-tab-item-title" :class="statusItem.status == refundStatus ? 'uni-tab-item-title-active color-base-border  color-base-text' : ''">
					{{ statusItem.name }}
				</text>
			</view>
		</scroll-view>
		
		<mescroll-uni ref="mescroll" @getData="getListData" top="100rpx">
			<block slot="list">
				<view class="container">
					<block v-if="refundList.length">
						<view class="order-item" v-for="(item, index) in refundList" :key="index">
							<view class="order-header">
								<text class="status-num font-size-base">{{ item.order_no }}</text>
								<view class="status-name" v-if="item.refund_status == 3">退款成功</view>
								<view class="status-name color-base-text" v-if="item.refund_status == 1">退款中</view>
								<view class="status-name color-base-text" v-if="item.refund_status == -1">退款失败</view>
							</view>
							<view class="goods-wrap">
								<image :src="$util.img(item.sku_image, { size: 'mid' })" @error="imageError(index)" mode="aspectFill"
								 :lazy-load="true"></image>
								<view class="goods-info">
									<view class="goods-name" @click="refundDetail(item.order_goods_id)">{{ item.sku_name }}</view>
									<view class="goods-num">
										<view class="num-text color-base-text">{{ item.refund_status_name }}</view>
										<view class="num-price">
											<text>￥{{ item.price }}</text>
											<text class="num">×{{ item.num }}</text>
										</view>
									</view>
								</view>
							</view>
							<view class="msg-address" v-if="item.refund_status == 8" @click="mapRoute(item.delivery_community_info)">
								<text class="address-title">取货地址</text>
								<text class="address-text">{{item.delivery_community_info.full_address}}</text>
								<text class="iconfont iconyouzhuanhuise"></text>
							</view>
							<view class="goods-btn">
								<view class="btn-text">
									<text>共{{ item.num }}件商品</text>
								</view>
								<view class="order-action">
									<block v-if="item.clerk_action.length">
										<view class="order-box-btn" @click="refundAction(actionItem.event, item)" v-for="(actionItem, actionIndex) in item.clerk_action"
										 :key="actionIndex">
											{{ actionItem.title }}
										</view>
									</block>
								</view>
							</view>
						</view>
					</block>
					<block v-else>
						<view class="cart-empty" v-if="showEmpty">
							<ns-empty :isIndex="!1" text="暂无售后记录"></ns-empty>
						</view>
					</block>
				</view>
			</block>
		</mescroll-uni>
			
		<loading-cover ref="loadingCover"></loading-cover>
	</view>
</template>

<script>
	import globalConfig from '@/common/js/golbalConfig.js';
	import Map from '@/common/js/map/openMap.js';
	
	export default {
		mixins: [globalConfig],
		data() {
			return {
				refundList: [],
				showEmpty: false,
				scrollInto: '',
				refundStatus: 'all',
				statusList: []
			};
		},
		onLoad() {
			this.getRefundStatus();
		},
		components: {},
		onShow() {
			// 刷新多语言
			this.$langConfig.refresh();

			if (!uni.getStorageSync('token')) {
				this.$util.redirectTo('/otherpages/order/login/login', {
					back: '/otherpages/community/clerk_activist'
				});
			}
		},
		methods: {
			ontabtap(e) {
				let index = e.target.dataset.current || e.currentTarget.dataset.current;
				this.refundStatus = this.statusList[index].status;
				if (this.refundStatus == '') this.refundList = [];
				this.$refs.loadingCover.show();
				this.$refs.mescroll.refresh();
			},
			getRefundStatus() {
				this.$api.sendRequest({
					url: '/api/orderrefund/refundstatus',
					success: res => {
						if (res.code == 0) this.statusList = res.data;
					}
				})
			},
			getListData(mescroll) {
				this.$api.sendRequest({
					url: '/api/communitydelivery/refundorder',
					data: {
						page: mescroll.num,
						page_size: mescroll.size,
						refund_status: this.refundStatus
					},
					success: res => {
						this.showEmpty = true;
						let newArr = [];
						let msg = res.message;
						if (res.code == 0 && res.data) {
							newArr = res.data.list;
						} 
						mescroll.endSuccess(newArr.length);
						//设置列表数据
						if (mescroll.num == 1) this.refundList = []; //如果是第一页需手动制空列表
						this.refundList = this.refundList.concat(newArr);
						this.$refs.loadingCover.hide();
					},
					fail: res => {
						mescroll.endErr();
						this.$refs.loadingCover.hide();
					}
				});
			},
			refundDetail(order_goods_id) {
				// this.$util.redirectTo('/otherpages/order/refund_detail/refund_detail', {
				// 	order_goods_id
				// });
			},
			refundAction(event, data) {
				switch (event) {
					case 'clerkRefundTakeDelivery':
						uni.showModal({
							content: '您确定已收到用户退回的货物了吗？',
							success: res => {
								if (res.confirm) {
									this.$api.sendRequest({
										url: '/api/orderrefund/clerkconfirm',
										data: {
											order_goods_id: data.order_goods_id
										},
										success: res => {
											this.$refs.mescroll.refresh();
										}
									})
								}
							}
						})
						break;
				}
			},
			imageError(index) {
				this.refundList[index].sku_image = this.$util.getDefaultImage().default_goods_img;
				this.$forceUpdate();
			},
			mapRoute(data) {
				Map.openMap(Number(data.latitude), Number(data.longitude), '', 'gcj02');
			}
		}
	};
</script>

<style lang="scss" scoped>
	/deep/ .fixed {
		position: relative;
		top: 0;
	}

	.cart-empty {
		padding-top: 154px !important;
	}

	@import './public/css/activist.scss';
	
	.order-item {
		.goods-wrap {
			border-bottom: unset;
		}
		
		.goods-btn {
			margin-top: 20rpx;
			padding-top: 20rpx;
			border-top: 1px solid #f7f7f7;
		}
		
		.msg-address {
			display: flex;
			align-items: flex-start;
			padding-bottom: $padding;
			padding-top: 10rpx;
		
			.address-title {
				font-size: $font-size-base;
				width: 200rpx;
			}
		
			.address-text {
				font-size: $font-size-sub;
				width: 480rpx;
				display: -webkit-box;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
				overflow: hidden;
				color: $color-sub;
		
			}
			
			.iconyouzhuanhuise {
				font-size: 46rpx;
				margin-left: 20rpx;
				line-height: 1.5;
				color: #5083f5;
			}
		}
	}
</style>
